<?php namespace system\model;
use houdunwang\model\Model;
class Article extends Model{
	//数据表
	protected $table = "article";

	//允许填充字段
	protected $allowFill = [ ];

	//禁止填充字段
	protected $denyFill = [ ];

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
//		['title','isnull','文章名不能为空',3,3],
//		['author','isnull','作者不能为空',3,3],
//		['category_cid','isnull','分类不能为空',3,3],
//		['digest','isnull','文章摘要不能为空',3,3],
//		['ckeywords','isnull','关键字不能为空',3,3],
//		['des','isnull','描述不能为空',3,3],
//		['content','isnull','内容不能为空',3,3],
	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
   //['sendtime','isnull','内容不能为空',3,3],

	];

	//自动过滤
    protected $filter=[
        //[表单字段名,过滤条件,处理时间]

    ];

	//时间操作,需要表中存在created_at,updated_at字段
	protected $timestamps=false;
//	获取文章列表和文章数据表的所有数据首页
	public function arc_article(){
		return Db::table('article')
			->join('category','article.category_cid','=','category.cid')->get();
	}
//	获取分类列表的所有数据并且变成树状结构
	public function getcatedata(){
		return Arr::tree(Db::table('category')->get(),'cname');
	}
//	获得标签呢列表的所有数据
	public function gettagdata(){
		$tag=Db::table('tag')->get();
		return $tag;
	}


//	添加
	public function arc_add(){
//		p($_POST);die;
		if(empty($_POST['tag'])){
			return ['valid'=>0,'message'=>"请选择标签"];
		}
		$this->validate=[
			['ctitle','isnull','文章名不能为空',3,3],
			['author','isnull','作者不能为空',3,3],
			['category_cid','isnull','分类不能为空',3,3],
			['digest','isnull','文章摘要不能为空',3,3],
//			['des','isnull','描述不能为空',3,3],
//			['content','isnull','内容不能为空',3,3],
//			['keyword','isnull','关键字不能为空',3,3]
		];
//			$ctitle=$_POST['ctitle'];
//		die;
//然后直接给数据对象赋值
//文章表中插入数据c
		$this['ctitle'] = $_POST['ctitle'];
		$this['author'] = $_POST['author'];
		$this['category_cid'] = $_POST['category_cid'];
		$this['thumb'] = $_POST['thumb'];
		$this['digest'] = $_POST['digest'];
//		$this['keyword'] = $_POST['keyword'];
//		$this['content'] = $_POST['content'];
//		$this['des'] = $_POST['des'];
		$this['sendtime']=date("y-m-d h:i:s");
//把数据对象添加到数据库
		$aid=$this->save();
		$this->validate=[
			['des','isnull','描述不能为空',3,3],
			['content','isnull','内容不能为空',3,3],
			['keyword','isnull','关键字不能为空',3,3]
		];
		$model=new Article_data();
		$model['des']=$_POST['des'];
		$model['content']=$_POST['content'];
		$model['keyword']=$_POST['keyword'];
		$model['article_aid']=$aid;
		$model->save();
		//遍历标签表里的标签
		foreach($_POST['tag'] as $v){
			Db::table('article_tag')->insert(['article_aid'=>$aid,'tag_tid'=>$v]);
		}
		return ['valid'=>1,'message'=>"执行成功"];

	}
//	编辑前先查表
	public function arc_edit($aid){
//		p($aid);die;
		return Db::table('article')->join('article_data','article.aid','=','article_data.article_aid')->where('article.aid',$aid)->first();
	}
//	现在编辑
	public function edit_bj(){
//		替换
//		p($_POST);die;
		$aid=$_GET['aid'];
		$this['ctitle'] = $_POST['ctitle'];
		$this['author'] = $_POST['author'];
		$this['category_cid'] = $_POST['category_cid'];
		$this['thumb'] = $_POST['thumb'];
		$this['digest'] = $_POST['digest'];
		$this['keyword'] = $_POST['keyword'];
		$this['content'] = $_POST['content'];
		$this['des'] = $_POST['des'];
//		$this['updatetime']=time();
		$updatetime=time();
//		插入表
		Db::table('article')->where("aid",$aid)->update([
			'ctitle'=>$_POST['ctitle'],
			'author'=>$_POST['author'],
			'category_cid'=>$_POST['category_cid'],
			'thumb'=>$_POST['thumb'],
			'digest'=>$_POST['digest'],
//			'keyword'=>$_POST['keyword'],
//			'content'=>$_POST['content'],
//			'des'=>$_POST['des'],
			'updatetime'=>$updatetime
		]);

		Db::table('article_data')->where("article_aid",$aid)->update([
			'keyword'=>$_POST['keyword'],
			'content'=>$_POST['content'],
			'des'=>$_POST['des'],
		]);
//			删除
		Db::table('article_tag')->where('article_aid',$aid)->delete();
		foreach($_POST['tag'] as $v){
//			增加
			Db::table('article_tag')->insert(['article_aid'=>$aid,'tag_tid'=>$v]);
		}
		return ['valid'=>1,'message'=>'修改成功'];
	}
//		回收站$num每页多少条 $is_recyle是不是在回收站
	public function getAlldata($num,$is_recyle){
//		统计文章的一共有多少条
		$count=Db::table('article')->count();
		p($count);
//		分页每页有多少数据$num，一共有多少数据$count
//		$num=9;
		$page=Page::row($num)->make($count);
//		Page::pageNum(2)->make($count);
//		找出所有的数据
		$data=Db::table('article')->join('category','category.cid','=','article.category_cid')
			->where('article.is_recyle',$is_recyle)->limit(Page::limit())
			->get();
//		p($data);die;
		return['page'=>$page,'data'=>$data];

	}
	public function del($aid){
//		删除文章表
		Db::table('article')->where('aid',$aid)->delete();
//		删除文章数据表
		(new Article_data())->where('article_aid',$aid)->delete();
//		删除标签表
		(new Article_tag())->where('article_aid',$aid)->delete();
		return true;
	}


}